﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Linq.Expressions;


namespace NS_Client.Data
{
    public static class Protocol
    {
        private static ProtocolFile ProtocolVals = new ProtocolFile();


        public static class Keys
        {
            public static string ProtocolCmd
            { get { return ProtocolVals.ProtocolCmd; } }

            public static string Username
            { get { return ProtocolVals.Username; } }

            public static string Password
            { get { return ProtocolVals.Password; } }

            public static string FileName
            { get { return ProtocolVals.FileName; } }

            public static string LoginKey
            { get { return ProtocolVals.LoginKey; } }

            public static string Software
            { get { return ProtocolVals.Software; } }

            public static string SoftwareName
            { get { return ProtocolVals.SoftwareName; } }

            public static string SoftwareDescription
            { get { return ProtocolVals.SoftwareDescription; } }

            public static string SoftwareID
            { get { return ProtocolVals.SoftwareID; } }

            public static string Name
            { get { return ProtocolVals.Name; } }

            public static string Description
            { get { return ProtocolVals.Description; } }

            public static string ID
            { get { return ProtocolVals.ID; } }

            public static string ApprovalStatus
            { get { return ProtocolVals.ApprovalStatus; } }

            public static string ChatText
            { get { return ProtocolVals.ChatText; } }

            public static string OwnerUsername
            { get { return ProtocolVals.OwnerUsername; } }

            public static string SoftwareImagePath
            { get { return ProtocolVals.SoftwareImagePath; } }

            public static string Messages
            { get { return ProtocolVals.Messages; } }
        }

        public static class Cmd_Values
        {
            public static string DownloadSoftware
            { get { return ProtocolVals.DownloadSoftware; } }

            public static string RequestFileUpload
            { get { return ProtocolVals.Password; } }

            public static string Login
            { get { return ProtocolVals.Login; } }            

            public static string FileUploadRequest
            { get { return ProtocolVals.FileUploadRequest; } }            

            public static string RequestStoreSoftwareList
            { get { return ProtocolVals.RequestStoreSoftwareList; } }

            public static string Confirmation
            { get { return ProtocolVals.Confirmation; } }

            public static string StoreSoftwareList
            { get { return ProtocolVals.StoreSoftwareList; } }

            public static string UserSoftwareList
            { get { return ProtocolVals.UserSoftwareList; } }

            public static string UserCreatedSoftwareList
            { get { return ProtocolVals.UserCreatedSoftwareList; } }

            public static string SendChatMessage
            { get { return ProtocolVals.SendChatMessage; } }

            public static string LoginSuccess
            { get { return ProtocolVals.LoginSuccess; } }

            public static string PollChat
            { get { return ProtocolVals.PollChat; } }

            public static string ChatMessages
            { get { return ProtocolVals.ChatMessages; } }
        }

        private class ProtocolFile
        {
            #region Default Initialization Values

            private Dictionary<string, string> ProtocolDict = new Dictionary<string, string>()
            {
                {"ProtocolCmd", "NS_PROTOCOL"                                 },
                {"Login", "Login"                                             },
                {"Username", "NS_USERNAME"                                    },
                {"Password", "NS_PASSWORD"                                    },
                {"RequestVersionUploadReply", "RequestVersionUploadReply"     },
                {"LoginKey", "NS_LOGINKEY"                                    },
                {"SoftwareVersionID", "NS_SOFTWAREVERSIONID"                  },
                {"RequestStoreSoftwareList", "RequestStoreSoftwareList"       },
                {"VersionNumber", "NS_VERSION"                                },
                {"DownloadSoftware", "DownloadSoftware"                       },
                {"SoftwareName", "NS_NAME"                                    },
                {"SoftwareDescription", "NS_DESCRIPTION"                      },
                {"SoftwareCategoryID", "NS_CATEGORY"                          },
                {"Filename", "NS_FILENAME"                                    },             
                {"FileUploadRequestReply", "FileUploadRequestReply"           },
                {"Confirmation", "NS_CONFIRMATION"                            },
                {"AuthFailed", "AuthFailed"                                   },
                {"SoftwareDescriptionCreation", "SoftwareDescriptionCreation" },
                {"AttemptUploadFile", "AttemptUploadFile"                     },
                {"Software", "NS_SOFTWARE"                                    },
                {"SoftwareID", "NS_SOFTWAREID"                                },
                {"StoreSoftwareList", "StoreSoftwareList"                     },
                {"UserSoftwareList", "UserSoftwareList"                       },
                {"UserCreatedSoftwareList", "userCreatedSoftwareList"         },
                {"ID", "ID"                                                   },
                {"ApprovalStatus", "ApprovalStatus"                           },
                {"ChatText", "NS_MESSAGE"                                     },
                {"SendChatMessage", "SendChatMessage"                         },
                {"LoginSuccess", "LoginSuccess"                               },
                {"OwnerUsername", "OwnerUsername"                             },
                {"SoftwareImagePath", "SoftwareImagePath"                     },   
                {"Name", "Name"                                               },
                {"Description", "Description"                                 },
                {"PollChat", "RequestChat"                                    },
                {"ChatMessages", "ChatMessages"                               },
                {"Messages", "NS_MESSAGES"                                    },
            };



            #endregion

            public ProtocolFile()
            {

                String ContextPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                char[] assembly_name = (System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".exe").ToCharArray();

                ContextPath = ContextPath.TrimEnd(assembly_name);

                // If no protocol file exists - create one with defualt values
                if (!File.Exists(ContextPath + "protocol.ns"))
                {
                    using (StreamWriter protocol_file_writer = File.CreateText(ContextPath + "protocol.ns"))
                    {

                        foreach (KeyValuePair<string, string> Protocol_Pair in ProtocolDict)
                        {
                            protocol_file_writer.Write(Protocol_Pair.Key + '=' + Protocol_Pair.Value + ',');
                        }

                        protocol_file_writer.Flush();
                    }
                }
                // Else Read in the protocol values from file
                else
                {
                    string protocol_file_contents = File.ReadAllText(ContextPath + "protocol.ns");
                    string[] protocol_KeyValPairs = protocol_file_contents.Split(',');

                    foreach (string Pair in protocol_KeyValPairs)
                    {
                        string[] KeyAndVal = Pair.Split('=');

                        if (ProtocolDict.Keys.Contains<string>(KeyAndVal[0]))
                            ProtocolDict[KeyAndVal[1]] = KeyAndVal[1];                       
                    }
                }
                // If reading error, alert user(?), delete file, create file with default values            
            }

            #region Public Members

            public string Messages
            {
                get
                {
                    return ProtocolDict["Messages"];
                }
            }

            public string ChatMessages
            {
                get
                {
                    return ProtocolDict["ChatMessages"];
                }
            }

            public string SoftwareImagePath
            {
                get
                {
                    return ProtocolDict["SoftwareImagePath"];
                }
            }

            public string PollChat
            {
                get
                {
                    return ProtocolDict["PollChat"];
                }
            }

            public string OwnerUsername
            {
                get
                {
                    return ProtocolDict["OwnerUsername"];
                }
            }
            public string LoginSuccess
            {
                get
                {
                    return ProtocolDict["LoginSuccess"];
                }
            }
            public string SendChatMessage
            {
                get
                {
                    return ProtocolDict["SendChatMessage"];
                }
            }
            public string ChatText
            {
                get
                {
                    return ProtocolDict["ChatText"];
                }
            }
            public string ID
            {
                get
                {
                    return ProtocolDict["ID"];
                }
            }
            public string Name
            {
                get
                {
                    return ProtocolDict["Name"];
                }
            }
            public string Description
            {
                get
                {
                    return ProtocolDict["Description"];
                }
            }
            public string ApprovalStatus
            {
                get
                {
                    return ProtocolDict["ApprovalStatus"];
                }
            }
            public string StoreSoftwareList
            {
                get
                {
                    return ProtocolDict["StoreSoftwareList"];
                }
            }
            public string UserCreatedSoftwareList
            {
                get
                {
                    return ProtocolDict["UserCreatedSoftwareList"];
                }
            }
            public string UserSoftwareList
            {
                get
                {
                    return ProtocolDict["UserSoftwareList"];
                }
            }
            public string Confirmation
            {
                get
                {
                    return ProtocolDict["Confirmation"];
                }
            }
            public string SoftwareID
            {
                get
                {
                    return ProtocolDict["SoftwareID"];
                }
            }
            public string Software
            {
                get
                {
                    return ProtocolDict["Software"];
                }
            }
            public string AttemptUploadFile
            {
                get
                {
                    return ProtocolDict["AttemptUploadFile"];
                }
            }
            public string FileName
            {
                get
                {
                    return ProtocolDict["Filename"];
                }
            }
            public string FileUploadRequest
            {
                get
                {
                    return ProtocolDict["FileUploadRequestReply"];
                }
            }
            public string VersionNumber
            {
                get
                {
                    return ProtocolDict["VersionNumber"];
                }
            }
            public string SoftwareName
            {
                get
                {
                    return ProtocolDict["SoftwareName"];
                }
            }
            public string SoftwareDescription
            {
                get
                {
                    return ProtocolDict["SoftwareDescription"];
                }
            }
            public string SoftwareCategoryID
            {
                get
                {
                    return ProtocolDict["SoftwareCategoryID"];
                }
            }
            public string Login
            {
                get
                {
                    return ProtocolDict["Login"];
                }
            }
            public string Username
            {
                get
                {
                    return ProtocolDict["Username"];
                }
                set {
                    ProtocolDict["Username"] = value;
                }
            }
            public string Password
            {
                get
                {
                    return ProtocolDict["Password"];
                }
            }
            public string ProtocolCmd
            {
                get
                {
                    return ProtocolDict["ProtocolCmd"];
                }
            }
            public string RequestStoreSoftwareList
            {
                get
                {
                    return ProtocolDict["RequestStoreSoftwareList"];
                }
            }
            public string LoginKey
            {
                get
                {
                    return ProtocolDict["LoginKey"];
                }
            }
            public string SoftwareVersionID
            {
                get
                {
                    return ProtocolDict["SoftwareVersionID"];
                }
            }
            public string DownloadSoftware
            {
                get
                {
                    return ProtocolDict["DownloadSoftware"];
                }
            }

            #endregion
        }
    }
}
